module repository.FriendApplyRepository;

import model;
import std.conv : to;
import bootstrap.DatabaseBootstrap;
import hunt.logging;
import hunt.entity;
import core.stdc.time;
import service.IdGeneratorService;

import hunt.framework;

class FriendApplyRepository: EntityRepository!( FriendApply , int)
{
    this()
    {
      super(defaultEntityManager());
    }

    FriendApply[] getUntreated(string toId)
    {
      scope(exit)
      {
        _manager.close();
      }
      auto query = _manager.createQuery!(FriendApply)("select * from FriendApply a where a.friendId = :friendId AND a.status = 0").
      setParameter("friendId", toId);
      FriendApply[] objs = query.getResultList();
      logInfo ("getUntreated size: %d",objs.length);
      return objs;
    }

    FriendApply[] getAll()
    {
      scope(exit)
      {
        _manager.close();
      }
      FriendApply[] objs = _manager.createQuery!(FriendApply)(" select * from FriendApply a where a.status = 1;").getResultList();
      logInfo ("size: %d",objs.length);
      return objs;
    }

    uint getChatId(int userId, int friendId)
    {
      scope(exit)
      {
        _manager.close();
      }
      auto query = _manager.createQuery!(FriendApply)("select * from FriendApply a where a.userId = :userId and a.friendId = :friendId;").
      setParameter("userId", userId).
      setParameter("friendId", friendId);
      FriendApply[] objs = query.getResultList();
      if(objs.length != 0)
      {
        return objs[0].id;
      }
      return 0;
    }

    uint putApplyContactReq(FriendApply req)
    {
        scope(exit)
        {
          _manager.close();
        }
        string id = IdGeneratorService.instance().getNextId();
       // _manager.createNativeQuery("set auto_increment_offset =1;set auto_increment_increment=2;");
        auto insert = _manager.createQuery!(FriendApply)("INSERT INTO FriendApply u(u.userId,u.friendId,u.status,u.created,u.updated,u.message,u.id,u.src) values (:userId,:friendId,:status,:created,:updated,:message,:id,:src);");
        insert.setParameter("userId",req.userId);
        insert.setParameter("friendId",req.friendId);
        insert.setParameter("status",req.status);
        insert.setParameter("created",req.created);
        insert.setParameter("updated",req.updated);
        insert.setParameter("message",req.message);
        insert.setParameter("id",to!uint(id));
        insert.setParameter("src",req.src);
        insert.exec();
        //RowSet rowset =  _manager.createNativeQuery("select MAX(id) from friend_apply;").getResultList();
        //
        //Row row =  rowset.iterator.front();
        return to!uint(id);
    }

    bool isApplyed(FriendApply req)
    {
        long c = count(new Condition("%s = %s and %s = %s and (%s = %s or %s = %s)" , Field.userId , req.userId,Field.friendId , req.friendId,Field.status , 0, Field.status , 1));
        return c == 0? true:false;
    }

    FriendApply findApply(FriendApply req)
    {
        return find(new Condition("%s = %s and %s = %s", Field.userId , req.userId, Field.friendId, req.friendId));
    }

    bool unDealed(int from, int to)
    {
        scope(exit)
        {
          _manager.close();
        }
        auto  query = _manager.createQuery!(FriendApply)(" select * from FriendApply a where a.userId = :from and a.friendId = :to and a.status = 0").
        setParameter("from", from).
        setParameter("to", to);
        FriendApply[] objs = query.getResultList();
        return (objs.length != 0) ? true : false;
    }

    void update (string chatId, int succeeded)
    {
      scope(exit)
      {
        _manager.close();
      }
      auto update = _manager.createQuery!(FriendApply)("update FriendApply u set u.status = :status, u.updated = :updated where  " ~
      "u.id = :id" );
      update.setParameter("status",succeeded);
      update.setParameter("updated",core.stdc.time.time(null));
      update.setParameter("id",to!int(chatId));
      try{
        update.exec();
      }
      catch(Exception e)
      {
        logError("updateStatus error : %s",e.msg);
      }
    }

    void updateAll (string chatId, string message, int src, int status)
    {
      scope(exit)
      {
        _manager.close();
      }
      auto update = _manager.createQuery!(FriendApply)("update FriendApply u set u.message = :message, u.updated = :updated, u.src = :src, u.status = :status where  " ~
      "u.id = :id" );
      update.setParameter("message",message);
      update.setParameter("updated",core.stdc.time.time(null));
      update.setParameter("src",src);
      update.setParameter("id",to!int(chatId));
      update.setParameter("status",status);
      try{
        update.exec();
      }
      catch(Exception e)
      {
        logError("updateMessage error : %s",e.msg);
      }
    }
}
